package io.kiki.sba.registry.client.event;

import com.alipay.lookout.api.Id;
import com.alipay.lookout.api.Lookout;
import com.alipay.lookout.api.composite.MixinMetric;
import io.kiki.sba.registry.client.api.ClientConfig;
import io.kiki.sba.registry.client.api.Configurator;
import io.kiki.sba.registry.client.api.EventSubscriber;
import io.kiki.sba.registry.client.api.Subscriber;
import io.kiki.sba.registry.client.api.model.Event;
import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.TimeUnit;

@Setter
@Getter
public class LookoutSubscriber implements EventSubscriber {


    private static final String METRIC_DATA_ID_NAME = "data_id";


    private static final String METRIC_INSTANCE_ID_NAME = "instance_id";


    private static final String METRIC_COUNT_NAME = "count";


    private static final String METRIC_TIME_NAME = "time";


    private static final String METRIC_SUCCESS_NAME = "success";


    private static final String REGISTRY_PROCESS_SUBSCRIBER = "registry.process.subscriber";


    private static final String REGISTRY_PROCESS_CONFIGURATOR = "registry.process.configurator";


    @Override
    public boolean isSync() {
        return false;
    }


    @Override
    public void onEvent(Event event) {
        if (null == event) {
            return;
        }
        Class eventClass = event.getClass();

        if (eventClass == SubscriberProcessEvent.class) {
            SubscriberProcessEvent subscriberProcessEvent = (SubscriberProcessEvent) event;
            Subscriber subscriber = subscriberProcessEvent.getSubscriber();
            if (null == subscriber) {
                return;
            }

            ClientConfig clientConfig = subscriberProcessEvent.getClientConfig();
            if (null == clientConfig) {
                return;
            }

            Id id = Lookout.registry().createId(REGISTRY_PROCESS_SUBSCRIBER)//
                    .withTag(METRIC_DATA_ID_NAME, subscriber.getDataId())//
                    .withTag(METRIC_INSTANCE_ID_NAME, clientConfig.getInstanceId());//
            MixinMetric mixinMetric = Lookout.registry().mixinMetric(id);

            mixinMetric.counter(METRIC_COUNT_NAME).inc();
            mixinMetric.timer(METRIC_TIME_NAME).record(subscriberProcessEvent.getEnd() - subscriberProcessEvent.getStart(), TimeUnit.MILLISECONDS);

            if (null == subscriberProcessEvent.getThrowable()) {
                mixinMetric.counter(METRIC_SUCCESS_NAME).inc();
            }
        } else
            if (eventClass == ConfiguratorProcessEvent.class) {
                ConfiguratorProcessEvent configuratorProcessEvent = (ConfiguratorProcessEvent) event;
                Configurator configurator = configuratorProcessEvent.getConfigurator();
                if (null == configurator) {
                    return;
                }

                ClientConfig clientConfig = configuratorProcessEvent.getClientConfig();

                Id id = Lookout.registry().createId(REGISTRY_PROCESS_CONFIGURATOR)//
                        .withTag(METRIC_DATA_ID_NAME, configurator.getDataId())//
                        .withTag(METRIC_INSTANCE_ID_NAME, clientConfig.getInstanceId());
                MixinMetric mixinMetric = Lookout.registry().mixinMetric(id);

                mixinMetric.counter(METRIC_COUNT_NAME).inc();
                mixinMetric.timer(METRIC_TIME_NAME).record(configuratorProcessEvent.getEnd() - configuratorProcessEvent.getStart(), TimeUnit.MILLISECONDS);

                if (null == configuratorProcessEvent.getThrowable()) {
                    mixinMetric.counter(METRIC_SUCCESS_NAME).inc();
                }
            }
    }
}
